import { defaultAlertState } from './state';
import { copyObject } from './index';

/**
 * vue状态同步更新中心
**/

/**
 * @description alert弹窗状态管理
 * @param state: state
 * @param newState: alertState|string; 传入值
**/
const alert = (state, newState) => {
  // 深拷贝一份新数据
  const target = copyObject(defaultAlertState);
  if (typeof newState === 'string') {
    // 如果传值为字符串，更新内容
    target.title = newState;
    target.show = true;
  } else if (typeof newState === 'object') {
    // 如果传值为对象，进行数据赋值
    const newData = copyObject(newState);
    for (let key in newData) {
      target[key] = newData[key];
    }
    target['show'] = true;
  }
  // 如果未传值，关闭弹窗
  state.alertState = target;
};

// 设置结算弹窗倒计时时间
const setResultOutTime = (state, time) => {
  state.resultOutTime = time;
};

export default {
  // 弹框方法
  alert,
  setResultOutTime
};
